/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SaveData;

/**
 *
 * @author VLAAD
 */
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class CharFile extends SaveData {

    private File mFile;
    private PrintWriter mWriter;
    private String mTextToFile;
    private FileMemento mVersion;
    private boolean state;

    @Override
    public void open() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    // default (output.txt)
    public CharFile() {
        mFile = new File("output.txt");
        this.state = false;
        readFromFile();
    }

    // for string with file name
    public CharFile(String fileName) {
        mFile = new File(fileName + ".txt");
        this.state = false;
        readFromFile();
    }
    // for string with file name
    public CharFile(String fileName, String rozsz) {
        mFile = new File(fileName + "."+rozsz);
        this.state = false;
        readFromFile();
    }

    // for file (e.g from JFileChooser)
    public CharFile(File tempFile) {
        mFile = tempFile;
        this.state = false;
        readFromFile();
    }
    
    private void readFromFile() {
        this.state = false;
        StringBuilder sb = new StringBuilder();
        String line = null;
        BufferedReader tempReader = null;
        try {
            tempReader = new BufferedReader(new FileReader(mFile));
            line = tempReader.readLine();
            while (line != null) {
                sb.append(line);
                sb.append("\n");
                line = tempReader.readLine();
            }
            tempReader.close();
        } catch (IOException ex) {
            System.out.println("Nie poszło otwieranie pliku");
        } finally {
            if (tempReader != null) {
                try {
                    tempReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        mTextToFile = sb.toString();
        mVersion = new FileMemento(mTextToFile);
        try {
            mWriter = new PrintWriter(this.mFile);
        } catch (FileNotFoundException e) {
            System.out.println("Nie pykło otwarcie do zapisu");
        }
    }

    @Override
    public void clear() {
        mTextToFile = "";
    }
    
    @Override
    public String showDataToSave() {
        return this.mTextToFile;
    }

    @Override
    public void write(String temp) {
        this.mVersion.changeVersion(this.mTextToFile);
        this.mTextToFile = this.mTextToFile + temp;
    }

    @Override
    public void close() {
        JFrame tempFrame = new JFrame();
        int n = JOptionPane.showConfirmDialog(tempFrame,
                "Would you like to save changes in" + mFile.getName(),
                "Question", JOptionPane.YES_NO_OPTION);
        if (JOptionPane.YES_OPTION == n) {
            mWriter.write(this.mTextToFile);
            mWriter.close();
        }
    }

    @Override
    public void backChanges() {
        if (null != this.mVersion.recoverVersion()) {
            String temp = this.mTextToFile;
            this.mTextToFile = this.mVersion.recoverVersion();
            this.mVersion.changeVersion(temp);
        }

    }

    private class FileMemento {
        private String fileVersion;

        public FileMemento(String versionFromFile) {
            fileVersion = versionFromFile;
        }

        void changeVersion(String versionToSave) {
            this.fileVersion = versionToSave;
        }

        String recoverVersion() {
            return this.fileVersion;
        }
    }

}

